<template>
    <div>
        <Card shadow>
            <div class="search-con search-con-top">
                <ButtonGroup>
                    <Button :disabled="hasAuthority('BaseAccountEdit}')?false:true"  class="search-btn" type="primary" @click="handleModal()">
                        <span>添加</span>
                    </Button>
                </ButtonGroup>
            </div>
            <Table :columns="columns" :data="data" :loading="loading">
                <template slot="action" slot-scope="{ row }">
                    <a :disabled="hasAuthority('BaseAccountEdit')?false:true"   @click="handleModal(row)">
                        编辑</a>&nbsp;
                    <Dropdown v-show="hasAuthority('BaseAccountEdit')" transfer ref="dropdown" @on-click="handleClick($event,row)">
                        <a href="javascript:void(0)">
                            <span>更多</span>
                            <Icon type="ios-arrow-down"></Icon>
                        </a>
                        <DropdownMenu slot="list">
                            <DropdownItem  name="remove">删除</DropdownItem>
                        </DropdownMenu>
                    </Dropdown>&nbsp;
                </template>
            </Table>
            <Page :total="pageInfo.total" :current="pageInfo.page" :page-size="pageInfo.limit" show-elevator show-sizer
                  show-total
                  @on-change="handlePage" @on-page-size-change='handlePageSize'></Page>
        </Card>
        <Modal v-model="modalVisible"
               :title="modalTitle"
               width="40"
               @on-cancel="handleReset">
            <div>
                <Form ref="form" :model="formItem" :rules="formItemRules" :label-width="100">
                    <FormItem label="用户Id" prop="userId">
                        <Input v-model="formItem.userId"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="账号标识：手机号、邮箱、 用户名、或第三方应用的唯一标识" prop="account">
                        <Input v-model="formItem.account"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="密码凭证：站内的保存密码、站外的不保存或保存token）" prop="password">
                        <Input v-model="formItem.password"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="账号类型:password-密码、mobile-手机号、email-邮箱、weixin-微信、weibo-微博、qq-等等" prop="accountType">
                        <Input v-model="formItem.accountType"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="账户域:@admin.com,@developer.com" prop="domain">
                        <Input v-model="formItem.domain"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="注册IP" prop="registerIp">
                        <Input v-model="formItem.registerIp"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="状态:0-禁用 1-启用 2-锁定" prop="status">
                        <Input v-model="formItem.status"  placeholder="请输入内容"></Input>
                    </FormItem>
                </Form>
                <div class="drawer-footer">
                    <Button type="default" @click="handleReset">取消</Button>&nbsp;
                    <Button type="primary" @click="handleSubmit" :loading="saving">保存</Button>
                </div>
            </div>
        </Modal>
    </div>
</template>

<script>
    import BaseAccount from '@/api/BaseAccount'

    export default {
        name: 'BaseAccount',
        data () {
            return {
                loading: false,
                saving: false,
                modalVisible: false,
                modalTitle: '',
                pageInfo: {
                    total: 0,
                    page: 1,
                    limit: 10
                },
                formItemRules: {
                    userId: [
                        {required: true, message: '用户Id不能为空', trigger: 'blur'}
                    ],
                    account: [
                        {required: true, message: '账号标识：手机号、邮箱、 用户名、或第三方应用的唯一标识不能为空', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '密码凭证：站内的保存密码、站外的不保存或保存token）不能为空', trigger: 'blur'}
                    ],
                    accountType: [
                        {required: true, message: '账号类型:password-密码、mobile-手机号、email-邮箱、weixin-微信、weibo-微博、qq-等等不能为空', trigger: 'blur'}
                    ],
                    domain: [
                        {required: true, message: '账户域:@admin.com,@developer.com不能为空', trigger: 'blur'}
                    ],
                    registerIp: [
                        {required: true, message: '注册IP不能为空', trigger: 'blur'}
                    ],
                    status: [
                        {required: true, message: '状态:0-禁用 1-启用 2-锁定不能为空', trigger: 'blur'}
                    ],
                },
                formItem: {
                    accountId: '',
                    userId: '',
                    account: '',
                    password: '',
                    accountType: '',
                    domain: '',
                    registerIp: '',
                    status: '',
                },
                columns: [
                    {
                        title: '用户Id',
                        key: 'userId',
                        width: 100
                    },
                    {
                        title: '账号标识：手机号、邮箱、 用户名、或第三方应用的唯一标识',
                        key: 'account',
                        width: 100
                    },
                    {
                        title: '密码凭证：站内的保存密码、站外的不保存或保存token）',
                        key: 'password',
                        width: 100
                    },
                    {
                        title: '账号类型:password-密码、mobile-手机号、email-邮箱、weixin-微信、weibo-微博、qq-等等',
                        key: 'accountType',
                        width: 100
                    },
                    {
                        title: '账户域:@admin.com,@developer.com',
                        key: 'domain',
                        width: 100
                    },
                    {
                        title: '注册IP',
                        key: 'registerIp',
                        width: 100
                    },
                    {
                        title: '状态:0-禁用 1-启用 2-锁定',
                        key: 'status',
                        width: 100
                    },
                    {
                        title: '操作',
                        slot: 'action',
                        fixed: 'right',
                        width: 120
                    }
                ],
                data: []
            }
        },
        methods: {
            handleModal (data) {
                if (data) {
                    this.modalTitle = '编辑登录账号'
                    this.formItem = Object.assign({}, this.formItem, data)
                } else {
                    this.modalTitle = '添加登录账号'
                }
                this.modalVisible = true
            },
            handleReset () {
                const newData = {
                    accountId: '',
                    userId: '',
                    account: '',
                    password: '',
                    accountType: '',
                    domain: '',
                    registerIp: '',
                    status: '',
                }
                this.formItem = newData
                //重置验证
                let form  = this.refs['form']
                form.resetFields()
                        this.modalVisible = false
                this.saving = false
            },
            handleSubmit () {
                let form  = this.refs['form']
                form.validate((valid) => {
                    if (valid) {
                        this.saving = true
                        if (this.formItem.accountId) {
                        BaseAccount.update(this.formItem).then(res => {
                            if (res.code === 0) {
                                this.$Message.success('保存成功')
                                this.handleReset()
                             }
                            this.handleSearch()
                        }).finally(() => {this.saving = false})
                    } else {
                       BaseAccount.add(this.formItem).then(res => {
                            this.handleReset()
                            this.handleSearch()
                            if (res.code === 0) {
                                this.$Message.success('保存成功')
                            }
                          }).finally(() => {this.saving = false})
                        }
                   }
                })
            },
            handleSearch (page) {
                if (page) {
                    this.pageInfo.page = page
                }
                this.loading = true
                BaseAccount.list({page: this.pageInfo.page, limit: this.pageInfo.limit}).then(res => {
                    this.data = res.data.records
                    this.pageInfo.total = parseInt(res.data.total)
                }).finally(() => {this.loading = false})
            },
            handlePage (current) {
                this.pageInfo.page = current
                this.handleSearch()
            },
            handlePageSize (size) {
                this.pageInfo.limit = size
                this.handleSearch()
            },
            handleRemove (data) {
               let modal = this.$Modal
               modal.confirm({
                    title: '确定删除吗？',
                    onOk: () => {
                        BaseAccount.remove(data.accountId).then(res => {
                            if (res.code === 0) {
                                this.pageInfo.page = 1;
                                this.$Message.success('删除成功');
                            }
                            this.handleSearch();
                        })
                    }
                })
            },
            handleClick (name, row) {
                switch (name) {
                    case 'remove':
                        this.handleRemove(row)
                        break
                }
            }
        },
        mounted: function () {
            this.handleSearch()
        }
    }
</script>
